package com.huanrong.ajax;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.huanrong.service.UserService;
import com.huanrong.util.RedisUtil;
import com.huanrong.util.ResponseUtil;
import com.huanrong.vo.JqGridPage;

import net.sf.json.JSONObject;

@RestController
@RequestMapping("/ajax")
public class TestAjax
{
    @Autowired
    private UserService userService;
    
    @Autowired
    private RedisUtil redisUtil;
    
    @Autowired
    private ResponseUtil responseUtil;
    
    @GetMapping(value = "/test", produces = "application/javascript; charset=UTF-8")
    public String test(HttpServletRequest request)
    {
        return "哈哈";
    }
    
    @PostMapping(value = "/add-redis", produces = "application/javascript; charset=UTF-8")
    public String addRedis(HttpServletRequest request)
    {
        redisUtil.saveItem("test", "00");
        return "success";
    }
    
    @GetMapping(value = "/listUser", produces = "application/javascript; charset=UTF-8")
    public String listUser(HttpServletRequest request)
    {
        
        return responseUtil.response(userService.list());
    }
    
    @GetMapping(value = "/addUser", produces = "application/javascript; charset=UTF-8")
    public String addUser(HttpServletRequest request)
    {
        return responseUtil.response(userService.add());
    }
    
    @GetMapping(value = "/list-page", produces = "application/javascript; charset=UTF-8")
    public String listPageUser(HttpServletRequest request)
    {
        JqGridPage jqGridPage = JqGridPage.processPage(request.getParameter("page"), request.getParameter("rows"));
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", userService.listCount(jqGridPage));
        map.put("rows", userService.listByPage(jqGridPage));
        return JSONObject.fromObject(map).toString();
    }
}
